from socket import *
from threading import Thread

udpSocket = None
ip = ''
port = 0

# 收数据
def recv():
  while True:
    recvInfo = udpSocket.recvfrom(1024)
    print(">>%s:%s"%(str(recvInfo[1]), recvInfo[0]))


# 发数据
def sendData():
  while True:
    sendInfo = input("<<请输入消息")
    udpSocket.sendto(sendInfo.encode('gbk'), (ip, port))


def main():
  
  global udpSocket
  udpSocket = socket(AF_INET, SOCK_DGRAM)
  udpSocket.bind(('', 8900))

  global ip, port
  ip = input('请输入ip: ')
  port = int(input('请输入端口: '))

  tr = Thread(target=recv)
  ts = Thread(target=sendData)

  tr.start()
  ts.start()

  tr.join()
  ts.join()


if __name__ == '__main__':
  main()